﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Company.Shell.Controls
{
    /// <summary>
    /// Corner.xaml 的交互逻辑 - 90度弯管控件
    /// </summary>
    public partial class Corner : UserControl
    {
        private DoubleAnimation flowAnimation;

        public Corner()
        {
            InitializeComponent();
            InitializeFlowAnimation();
        }

        /// <summary>
        /// 弯管方向：0=左下到右上，1=右下到左上，2=右上到左下，3=左上到右下
        /// </summary>
        public int Direction
        {
            get { return (int)GetValue(DirectionProperty); }
            set { SetValue(DirectionProperty, value); }
        }

        public static readonly DependencyProperty DirectionProperty =
            DependencyProperty.Register("Direction", typeof(int), typeof(Corner),
                new PropertyMetadata(0, new PropertyChangedCallback(DirectionChangedCallback)));

        private static void DirectionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Corner)d;
            if (control == null) return;

            if (e.NewValue is int direction)
            {
                control.UpdateDirection(direction);
            }
        }

        /// <summary>
        /// 是否显示流体
        /// </summary>
        public bool ShowFluid
        {
            get { return (bool)GetValue(ShowFluidProperty); }
            set { SetValue(ShowFluidProperty, value); }
        }

        public static readonly DependencyProperty ShowFluidProperty =
            DependencyProperty.Register("ShowFluid", typeof(bool), typeof(Corner),
                new PropertyMetadata(false, new PropertyChangedCallback(ShowFluidChangedCallback)));

        private static void ShowFluidChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Corner)d;
            if (control == null) return;

            if (e.NewValue is bool showFluid)
            {
                control._fluidIndicator.Visibility = showFluid ? Visibility.Visible : Visibility.Collapsed;
                control._flowArrow.Visibility = showFluid ? Visibility.Visible : Visibility.Collapsed;

                if (showFluid)
                {
                    control.StartFlowAnimation();
                }
                else
                {
                    control.StopFlowAnimation();
                }
            }
        }

        /// <summary>
        /// 流体颜色
        /// </summary>
        public Brush FluidColor
        {
            get { return (Brush)GetValue(FluidColorProperty); }
            set { SetValue(FluidColorProperty, value); }
        }

        public static readonly DependencyProperty FluidColorProperty =
            DependencyProperty.Register("FluidColor", typeof(Brush), typeof(Corner),
                new PropertyMetadata(new SolidColorBrush(Color.FromRgb(64, 128, 255)), 
                    new PropertyChangedCallback(FluidColorChangedCallback)));

        private static void FluidColorChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Corner)d;
            if (control == null) return;

            if (e.NewValue is Brush brush)
            {
                control._fluidIndicator.Fill = brush;
            }
        }

        private void UpdateDirection(int direction)
        {
            var transform = new RotateTransform();
            var centerX = this.Width / 2;
            var centerY = this.Height / 2;

            switch (direction)
            {
                case 0: // 左下到右上（默认）
                    transform.Angle = 0;
                    break;
                case 1: // 右下到左上
                    transform.Angle = 90;
                    break;
                case 2: // 右上到左下
                    transform.Angle = 180;
                    break;
                case 3: // 左上到右下
                    transform.Angle = 270;
                    break;
            }

            transform.CenterX = centerX;
            transform.CenterY = centerY;
            this.RenderTransform = transform;
        }

        private void InitializeFlowAnimation()
        {
            flowAnimation = new DoubleAnimation()
            {
                From = 0.3,
                To = 0.8,
                Duration = TimeSpan.FromSeconds(1.5),
                AutoReverse = true,
                RepeatBehavior = RepeatBehavior.Forever
            };
        }

        private void StartFlowAnimation()
        {
            _fluidIndicator.BeginAnimation(OpacityProperty, flowAnimation);
        }

        private void StopFlowAnimation()
        {
            _fluidIndicator.BeginAnimation(OpacityProperty, null);
        }
    }
}
